package com.midea.oms;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Slf4j
public class PushOmsTest {

    public static final String SRC = "D:/MyData/guozl22/Desktop/pushOms.txt";
    public static final String ERROR = "D:/MyData/guozl22/Desktop/error.txt";

    public static void main(String[] args) throws IOException {
        List<String> list = FileUtils.readLines(new File(SRC), "UTF-8");

        list.forEach(param -> {
            PushOmsReq pushOmsReq = JSONUtil.toBean(JSONUtil.toJsonStr(param), PushOmsReq.class);
//            String[] params = param.split("\\|");
//            PushOmsReq pushOmsReq = new PushOmsReq();
//            pushOmsReq.setBusinessTime(params[0]);
//            pushOmsReq.setOperator(params[1]);
//            pushOmsReq.setOperatorTime(params[2]);
//            pushOmsReq.setOrderStatus("70");
//            pushOmsReq.setSourceSystem("TMS");
//            pushOmsReq.setTaskNo(params[3]);
//            pushOmsReq.setWorkOrderNo(params[4]);

            push(pushOmsReq);
        });

    }

    private static void push(PushOmsReq pushOmsReq) {
        String requestUrl="http://10.18.34.40:10123/logisticsRoute";
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        OkHttpClient okHttpClient = new OkHttpClient();
        String requestParam = JSONUtil.toJsonPrettyStr(pushOmsReq);
        final Request request = new Request.Builder()
            .url(requestUrl)
            .post(RequestBody.create(mediaType, JSONUtil.toJsonStr(pushOmsReq)))
            .build();
        final Call call = okHttpClient.newCall(request);
        try {
            Response resp = call.execute();
            HttpBody httpBody = JSONUtil.toBean(resp.body().string(), HttpBody.class);
            if ("0".equals(httpBody.getCode())) {
                // 成功
                System.out.println("success:" + requestParam);
            } else {
                // 失败
                System.out.println("fail:" + requestParam + "\n" + resp.body().string());
                String append = "fail:"+JSONUtil.toJsonStr(pushOmsReq) + "\n";
                FileUtil.appendString(append, ERROR, "utf-8");
            }
        } catch (Exception e) {
            System.out.println("exception:" + e.getMessage());
            String append = "exception:"+JSONUtil.toJsonStr(pushOmsReq) + "\n";
            FileUtil.appendString(append, ERROR, "utf-8");
        }

    }

    @ToString
    @Data
    public class PushOmsReq {

        private String businessTime;
        private String operator;
        private String operatorTime;
        private String orderStatus;
        private String sourceSystem;
        private String taskNo;
        private String workOrderNo;

    }

    @Data
    public class HttpBody {

        private String code;

        private String msg;

    }


}
